<?php
/**
 * Clase configuradora
 * Descripcion:
 * Esta clase permite detectar y cambiar la configuracion de tema, lenguaje, base de datos y otros
 * Escrito por: frimost (Armandux)
 * Ultima actualizacion: 28-01-2007
 */
class Setter{
	private $configurationsPath;//Ruta del directorio configurations
	private $file;
	
	//-----Atributos para la GUI (idioma y theme)
	private $theme;
	private $themesAvailable;
	private $lang;
	private $langsAvailable;//Idiomas disponibles

	//-----Atributos para conexion a base de datos
	private $host;
	private $dbName;
	private $userName;
	private $password;

	//----Atributos de la información de la compañia
	private $companyName;
	private $companyId;
	private $companyCountry;
	private $companyCity;
	private $companyAdress;
	private $companyPhone;
	private $companyWebSite;
	private $companyEmail;
	private $manager;
	private $managerAdmin;
	
	//----Atributos para las secciones de la aplicación
	private $sections; 

	
	//----Atributos Operativos
	private $porcentajeGanaciaArriendos;
	
	//Constructor y Destructor
	function __construct($configurationsPath="Config"){
		$this->configurationsPath=$configurationsPath;
		$this->file=array();
		
		//---GUI----------------------------------------
		$this->theme="standar";
		$this->lang="es";
		$this->langsAvailable=array();
		//-------------------------------------------------
		
		//---Company Info-------------------------
		$this->companyName="";
		$this->companyId="";
		$this->companyCountry="";
		$this->companyCity="";
		$this->companyAdress="";
		$this->companyPhone="";
		$this->companyWebSite="";
		$this->companyEmail="";
		$this->manager="";
		$this->managerAdmin="";
		//-------------------------------------------------
		
		//---Data Base------------------------------
		$this->host="";
		$this->dbName="";
		$this->userName="";
		$this->password="";
		//-------------------------------------------------
		
		//--Sections--------------------------------
		$this->sections=array();
		//------------------------------------------
		
		//--Operativos------------------------------
		$this->porcentajeGanaciaArriendos="";
		//------------------------------------------
	}
	
	function __destruct(){
		unset($this->configurationsPath);
		unset($this->file);
		
		//---GUI----------------------------------------
		unset($this->theme);
		unset($this->lang);
		unset($this->langsAvailable);
		//-------------------------------------------------
		
		//---Company Info-------------------------
		unset($this->companyName);
		unset($this->companyId);
		unset($this->companyCountry);
		unset($this->companyCity);
		unset($this->companyAdress);
		unset($this->companyPhone);
		unset($this->companyWebSite);
		unset($this->companyEmail);
		unset($this->manager);
		unset($this->managerAdmin);
		//-------------------------------------------------
		
		//---Data Base------------------------------
		unset($this->host);
		unset($this->dbName);
		unset($this->userName);
		unset($this->password);
		//-------------------------------------------------
		
		//--Sections--------------------------------
		unset($this->sections);
		//------------------------------------------
	}
	//Fin Constructor y Destructor

	//Get y Set
	function getTheme(){
		$this->file=$this->configurationsPath."/guiConfig.php";
		$config=file($this->file);
		$this->theme=trim($config[2],"\x00..\x1F");//\x00..\x1F es el grupo de caracteres de sistema que se quieren quitar :)
		return $this->theme;
	}
	function getAvailableThemes($themesPath="themes"){
		$themesDisp=scandir($themesPath);
		$this->themesAvailable = array_slice($themesDisp, 2);
		return $this->themesAvailable;
	}
	function getLanguage(){
		$this->file=$this->configurationsPath."/guiConfig.php";
		$config=file($this->file);
		$this->lang=trim($config[1],"\x00..\x1F");//\x00..\x1F es el grupo de caracteres de sistema que se quieren quitar :)
		return $this->lang;
	}
	function getAvailableLanguage($languagesPath="languages"){
		$langs=scandir($languagesPath);
		
		foreach($langs as $value){
			$langsDisp[]=substr($value,0,-4);
		}
		$this->langsAvailable = array_slice($langsDisp, 2);
		return $this->langsAvailable;
	}
	function getDataBaseHost(){
		$this->file=$this->configurationsPath."/dbConfig.php";
		$config=file($this->file);
		
		$this->host=trim($config[1],"\x00..\x1F");//\x00..\x1F es el grupo de caracteres de sistema que se quieren quitar :)
		return $this->host;
	}
	function getDataBaseName(){
		$this->file=$this->configurationsPath."/dbConfig.php";
		$config=file($this->file);
		
		$this->dbName=trim($config[2],"\x00..\x1F");//\x00..\x1F es el grupo de caracteres de sistema que se quieren quitar :)
		return $this->dbName;		
	}
	function getDataBaseUserName(){
		$this->file=$this->configurationsPath."/dbConfig.php";
		$config=file($this->file);
		
		$this->userName=trim($config[3],"\x00..\x1F");//\x00..\x1F es el grupo de caracteres de sistema que se quieren quitar :)
		return $this->userName;
	}
	function getDataBasePassword(){
		$this->file=$this->configurationsPath."/dbConfig.php";
		$config=file($this->file);
		$this->password=trim($config[4],"\x00..\x1F");//\x00..\x1F es el grupo de caracteres de sistema que se quieren quitar :)
		return $this->password;	
	}
	
	function getSections(){
		$this->file=$this->configurationsPath."/sectionsConfig.php";
		$config=file($this->file);
		foreach($config as $value){
			$this->sections[]=trim($value,"\x00..\x1F");//\x00..\x1F es el grupo de caracteres de sistema que se quieren quitar :)
		}
		array_shift($this->sections);
		return $this->sections;
	}
	function getPorcentajeGanaciaArriendos(){
		$this->file=$this->configurationsPath."/porcentajeGananciaArriendosConfig.php";
		$config=file($this->file);
		$this->porcentajeGanaciaArriendos=trim($config[1],"\x00..\x1F");//\x00..\x1F es el grupo de caracteres de sistema que se quieren quitar :)
		return $this->porcentajeGanaciaArriendos;	
	}
	
	function setTheme($themeSelected="standar"){
		//Leer el archivo de configuración
		$topogConfig=file($this->configurationsPath."/guiConfig.php");
		//----------------------------------
		//Cambiar la info tomada del archivo-------------
		$topogConfig[2]=$themeSelected."\n";
		//-----------------------------------------------
		//Eliminar el archivo con la info antigua
		unlink($this->configurationsPath."/guiConfig.php");
		//-----------------------------------------------
		//Crear archivo con la info nueva----------------
		$gestor=fopen($this->configurationsPath."/guiConfig.php",'x+');
		
		foreach($topogConfig as $value){
			$content=$content.$value;
		}
		fwrite($gestor,$content);
		fclose($gestor);
		//-----------------------------------------------
	}
	function setLanguage($langSelected="es"){
		//Leer el archivo de configuración
		$config=file($this->configurationsPath."/guiConfig.php");
		//----------------------------------
		//Cambiar la info tomada del archivo-------------
		$config[1]=$langSelected."\n";
		//-----------------------------------------------
		//Eliminar el archivo con la info antigua
		unlink($this->configurationsPath."/guiConfig.php");
		//-----------------------------------------------
		//Crear archivo con la info nueva----------------
		$gestor=fopen($this->configurationsPath."/guiConfig.php",'x+');
		
		foreach($config as $value){
			$content=$content.$value;
		}
		fwrite($gestor,$content);
		fclose($gestor);
		//-----------------------------------------------
	}
	function setDataBase($host="localhost", $dbName="", $userName="root", $password=""){
		//Leer el archivo de configuración
		$dbConfig=file($this->configurationsPath."/dbConfig.php");
		//----------------------------------
		//Cambiar la info tomada del archivo-------------
		$dbConfig[1]=$host."\n";
		$dbConfig[2]=$dbName."\n";
		$dbConfig[3]=$userName."\n";
		$dbConfig[4]=$password."\n";
		//-----------------------------------------------
		//Eliminar el archivo con la info antigua
		unlink($this->configurationsPath."/dbConfig.php");
		//-----------------------------------------------
		//Crear archivo con la info nueva----------------
		$gestor=fopen($this->configurationsPath."/dbConfig.php",'x+');
		
		foreach($dbConfig as $value){
			$content=$content.$value;
		}
		fwrite($gestor,$content);
		fclose($gestor);
		//-----------------------------------------------
	}
	
	function getCompanyName(){
		$this->file=$this->configurationsPath."/companyInfoConfig.php";
		$companyInfoConfig=file($this->file);
		$this->companyName=trim($companyInfoConfig[1],"\x00..\x1F");//\x00..\x1F es el grupo de caracteres de sistema que se quieren quitar :);
		return $this->companyName;
	}
	function getCompanyId(){
		$this->file=$this->configurationsPath."/companyInfoConfig.php";
		$companyInfoConfig=file($this->file);
		$this->companyId=trim($companyInfoConfig[2],"\x00..\x1F");//\x00..\x1F es el grupo de caracteres de sistema que se quieren quitar :);
		return $this->companyId;
	}
	function getCompanyCountry(){
		$this->file=$this->configurationsPath."/companyInfoConfig.php";
		$companyInfoConfig=file($this->file);
		$this->companyCountry=trim($companyInfoConfig[3],"\x00..\x1F");//\x00..\x1F es el grupo de caracteres de sistema que se quieren quitar :);
		return $this->companyCountry;
	}
	function getCompanyCity(){
		$this->file=$this->configurationsPath."/companyInfoConfig.php";
		$companyInfoConfig=file($this->file);
		$this->companyCity=trim($companyInfoConfig[4],"\x00..\x1F");//\x00..\x1F es el grupo de caracteres de sistema que se quieren quitar :);
		return $this->companyCity;
	}
	function getCompanyAdress(){
		$this->file=$this->configurationsPath."/companyInfoConfig.php";
		$companyInfoConfig=file($this->file);
		$this->companyAdress=trim($companyInfoConfig[5],"\x00..\x1F");//\x00..\x1F es el grupo de caracteres de sistema que se quieren quitar :);
		return $this->companyAdress;
	}
	function getCompanyPhone(){
		$this->file=$this->configurationsPath."/companyInfoConfig.php";
		$companyInfoConfig=file($this->file);
		$this->companyPhone=trim($companyInfoConfig[6],"\x00..\x1F");//\x00..\x1F es el grupo de caracteres de sistema que se quieren quitar :);
		return $this->companyPhone;
	}
	function getCompanyWebSite(){
		$this->file=$this->configurationsPath."/companyInfoConfig.php";
		$companyInfoConfig=file($this->file);
		$this->companyWebSite=trim($companyInfoConfig[7],"\x00..\x1F");//\x00..\x1F es el grupo de caracteres de sistema que se quieren quitar :);
		return $this->companyWebSite;
	}
	function getCompanyEmail(){
		$this->file=$this->configurationsPath."/companyInfoConfig.php";
		$companyInfoConfig=file($this->file);
		$this->companyEmail=trim($companyInfoConfig[8],"\x00..\x1F");//\x00..\x1F es el grupo de caracteres de sistema que se quieren quitar :);
		return $this->companyEmail;
	}
	function getCompanyManager(){
		$this->file=$this->configurationsPath."/companyInfoConfig.php";
		$companyInfoConfig=file($this->file);
		$this->manager=trim($companyInfoConfig[9],"\x00..\x1F");//\x00..\x1F es el grupo de caracteres de sistema que se quieren quitar :);
		return $this->manager;
	}
	function getCompanyManagerAdmin(){
		$this->file=$this->configurationsPath."/companyInfoConfig.php";
		$companyInfoConfig=file($this->file);
		$this->managerAdmin=trim($companyInfoConfig[10],"\x00..\x1F");//\x00..\x1F es el grupo de caracteres de sistema que se quieren quitar :);
		return $this->managerAdmin;
	}
		
	function setCompanyName($companyName=""){
		$this->companyName=$companyName;
	}
	function setCompanyId($companyId=""){
		$this->companyId=$companyId;
	}
	function setCompanyCountry($companyCountry=""){
		$this->companyCountry=$companyCountry;
	}
	function setCompanyCity($companyCity=""){
		$this->companyCity=$companyCity;
	}
	function setCompanyAdress($companyAdress=""){
		$this->companyAdress=$companyAdress;
	}
	function setCompanyPhone($companyPhone=""){
		$this->companyPhone=$companyPhone;
	}
	function setCompanyWebSite($companyWebSite=""){
		$this->companyWebSite=$companyWebSite;
	}
	function setCompanyEmail($companyEmail=""){
		$this->companyEmail=$companyEmail;
	}
	function setCompanyManager($companyManager=""){
		$this->manager=$companyManager;
	}
	function setCompanyManagerAdmin($companyManagerAdmin=""){
		$this->managerAdmin=$companyManagerAdmin;
	}
	/**Almacena la información completa de la empresa*/
	function setCompanyInfo(){
		//Leer el archivo de configuración
		$companyInfoConfig=file($this->configurationsPath."/companyInfoConfig.php");
		//----------------------------------
		//Cambiar la info tomada del archivo-------------
		$companyInfoConfig[1]=$this->companyName."\n";
		$companyInfoConfig[2]=$this->companyId."\n";
		$companyInfoConfig[3]=$this->companyCountry."\n";
		$companyInfoConfig[4]=$this->companyCity."\n";
		$companyInfoConfig[5]=$this->companyAdress."\n";
		$companyInfoConfig[6]=$this->companyPhone."\n";
		$companyInfoConfig[7]=$this->companyWebSite."\n";
		$companyInfoConfig[8]=$this->companyEmail."\n";
		$companyInfoConfig[9]=$this->manager."\n";
		$companyInfoConfig[10]=$this->managerAdmin."\n";
		//-----------------------------------------------
		//Eliminar el archivo con la info antigua
		unlink($this->configurationsPath."/companyInfoConfig.php");
		//-----------------------------------------------
		//Crear archivo con la info nueva----------------
		$gestor=fopen($this->configurationsPath."/companyInfoConfig.php",'x+');
		
		foreach($companyInfoConfig as $value){
			$content=$content.$value;
		}
		fwrite($gestor,$content);
		fclose($gestor);
		//-----------------------------------------------
	}
	
	function setPorcentajeGananciaArriendos($porcentaje="20"){
		//Leer el archivo de configuración
		$dbConfig=file($this->configurationsPath."/porcentajeGananciaArriendosConfig.php");
		//----------------------------------
		//Cambiar la info tomada del archivo-------------
		$dbConfig[1]=$porcentaje."\n";
		//-----------------------------------------------
		//Eliminar el archivo con la info antigua
		unlink($this->configurationsPath."/porcentajeGananciaArriendosConfig.php");
		//-----------------------------------------------
		//Crear archivo con la info nueva----------------
		$gestor=fopen($this->configurationsPath."/porcentajeGananciaArriendosConfig.php",'x+');
		
		foreach($dbConfig as $value){
			$content=$content.$value;
		}
		fwrite($gestor,$content);
		fclose($gestor);
		//-----------------------------------------------
	}
	//Fin Get y Set
}
?>